package server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

/**
 * @file Server.java
 *
 * @version 0.5
 * @author James Dibble 10009689
 */
public class Server extends Thread {
    // <editor-fold defaultstate="collapsed" desc="Attributes">

    private ServerSocket ssMySocket;
    private static ArrayList<Client_Handler> clients;
    public static boolean bolServerAlive, bolServerDebug;
    private Server_Type serverType;
    private Resources resources;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Constructors">
    /**
     * Create a new file server.
     *
     * @param intPort
     */
    public Server(int intPort, Server_Type serverType) throws IOException {
        this.ssMySocket = new ServerSocket(intPort);
        Server.clients = new ArrayList<>();
        Server.bolServerAlive = true;
        this.serverType = serverType;
        resources = new Resources();
        bolServerDebug = false;
        
        //ADD RESOURCES
        resources.AddResource(new Resource(1, "myProgram.jar", "A computer program", "The program displays a message", "newProgram.jar"));
        resources.AddResource(new Resource(2, "computer-recovery.jpg", "A Jpeg picture", "This picture shows …", "newPicture.jpg"));
        resources.AddResource(new Resource(3, "myBook.txt", "An e-book", "The book is about …", "newBook.txt"));
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Methods">
    @Override
    public void run() {
        while (bolServerAlive && !ssMySocket.isClosed()) {
            try {
                Socket newClientSocket = ssMySocket.accept();
                System.out.println(String.format("Connection recieved from %s(%s).",
                        newClientSocket.getInetAddress().getHostName(),
                        newClientSocket.getInetAddress().toString()));
                Client_Handler newClient = new Client_Handler(newClientSocket, this, bolServerDebug);
                new Thread(newClient).start();
                clients.add(newClient);
            } catch (IOException ex) {
                if (ex.getCause() != null) {
                    if(bolServerDebug)ex.printStackTrace(System.out);
                    System.out.println("Could not accept new Client.");
                }
            }
        }
    }

    public synchronized void KillServer() throws IOException {
        bolServerAlive = false;
        ssMySocket.close();
        this.interrupt();
        System.out.println("Server Killed.");
    }

    public Resource getResourceByID(int ID) {
        return this.resources.getResourceByID(ID);
    }

    public Server_Type getServerType() {
        return this.serverType;
    }

    public String listResources() {
        return resources.listResources();
    }

    public void toggleDebug() {
        if(bolServerDebug) bolServerDebug = false;
        else bolServerDebug = true;
        for(Client_Handler thisClient : clients){
            thisClient.bolDebug = bolServerDebug;
        }
        System.out.println("Debug mode: " + bolServerDebug);
    }

    public enum Server_Type {
        FILE_SERVER, DESCRIPTION_SERVER
    }
    // </editor-fold>
}
